<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\lottery\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use app\admin\model\AdminMenu;
use app\admin\model\Catalog;
use app\admin\model\Config as ConfigModel;
use plugins\lottery\admin\model\Lottery as LotteryModel;
/**
 * 抽奖记录管理
 */
class Lottery extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input = input("post.");
            $search = ["keyword", "type", 'date'];
            $append = ["receiver"];
            $count = LotteryModel::withSearch($search, $input)->count();
            $data  = LotteryModel::withSearch($search, $input)->with(['user'])->append($append)->order($input["prop"], $input["order"])->page($input["page"], $input["pageSize"])->select();
            return json(["status" => "success", "message" => "请求成功", "data" => $data, "count" => $count]);
        } else {
            return View::fetch();
        }
    }
    
    /**
     * 发货
     */
    public function deliverGoods()
    {
        if ($this->request->isPost()) {
            LotteryModel::update(input("post."));
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }
    
    /**
     * 备注
     */
    public function message()
    {
        if ($this->request->isPost()) {
            LotteryModel::update(input("post."));
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }
    
    /**
     * 消耗抽奖
     */
    public function config()
    {
        $name = 'app_lottery';
    	if ($this->request->isPost()) {
            $message = ConfigModel::setVal($name, '抽奖', input('post.config'));
            return json($message);
        } else {
            $config = ConfigModel::getVal($name);
            if (! $config) {
                $config = [
                    'title'      => '',
                    'time'       => [date('Y-m-d H:i:s'), '2026-12-01 00:00:00'],
                    'number'     => 2,
                    'prize'      => [],
                    'background' => '',
                    'rule'       => '',
                    'status'     => 0,
                ];
            }
            View::assign([
                'catalog' => [],
                'config'  => $config,
            ]);
            return View::fetch();
        }
    }
}